package com.example.async.controller;

import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.async.service.TaskService;

@RestController
@RequestMapping("test")
public class TaskController {

	@Autowired
	private TaskService service;

	@GetMapping("asyncNonBlocking")
	public String asyncNonBlocking() {
		// 异步执行任务
		service.executeAsyncNonBlocking();
		return "异步任务（非阻塞）已完成！";
	}

	@GetMapping("async")
	public String async() {
		// 异步执行任务
		try {
			service.executeAsync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return "异步任务已完成！";
	}

	@GetMapping("sync")
	public String sync() {
		// 同步执行任务
		service.executeSync();
		return "同步任务已完成！";
	}

}
